/**
* TG_Coder: Dzlua
* Email: 505544956@qq.com
* Time: 2017/05/16
*/
#include "TGPath.h"

#define arrysize(a) (sizeof(a) / sizeof(*(a)))

#pragma comment(lib, "Advapi32.lib")

std::string TGPath::GetRegValue(HKEY hKey, const std::string& strUrl, const std::string& strKey)
{
    std::string strValue;
    HKEY  hKeyResult = NULL;
    DWORD dwSize     = 0;
    DWORD dwDataType = 0;

    //open
    if(ERROR_SUCCESS != ::RegOpenKeyExA(hKey, strUrl.c_str(), 0, KEY_QUERY_VALUE, &hKeyResult))
        return strValue;

    //get size and type
    ::RegQueryValueExA(hKeyResult, strKey.c_str(), 0, &dwDataType, NULL, &dwSize);

    switch (dwDataType) {
        case REG_MULTI_SZ: {
            BYTE* lpValue = new BYTE[dwSize];
            LONG lRet = ::RegQueryValueExA(hKeyResult, strKey.c_str(), 0, &dwDataType, lpValue, &dwSize);
            delete[] lpValue;
        } break;
        case REG_SZ: {
            char* lpValue = new char[dwSize];
            memset(lpValue, 0, dwSize * sizeof(char));
            if (ERROR_SUCCESS == ::RegQueryValueExA(hKeyResult, strKey.c_str(), 0, &dwDataType, (LPBYTE)lpValue, &dwSize)) {
                strValue = lpValue;
            }
            delete[] lpValue;
        } break;
        default : break;
    }

    //close
    ::RegCloseKey(hKeyResult);
    return strValue;
}

//D:\Program Files (x86)\TGGame
std::string TGPath::GetInstallPath()
{
    std::string strUrl = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Tango3.exe";
    std::string strKey = "";
    HKEY hKeys[] = {
        HKEY_CLASSES_ROOT,
        HKEY_CURRENT_USER,
        HKEY_LOCAL_MACHINE,
        HKEY_USERS,
        HKEY_PERFORMANCE_DATA,
        HKEY_CURRENT_CONFIG,
        HKEY_DYN_DATA,
        HKEY_CURRENT_USER_LOCAL_SETTINGS,
        HKEY_PERFORMANCE_TEXT,
        HKEY_PERFORMANCE_NLSTEXT
    };

    std::string strValue;
    int size = arrysize(hKeys);
    for (int i = 0; i < size; ++i) {
        strValue = TGPath::GetRegValue(hKeys[i], strUrl, strKey);
        if (!strValue.empty())
            break;
    }

    if (strValue.empty())
        return strValue;

    std::string tango = "Tango3.exe";
    return strValue.replace(strValue.find(tango), tango.length(), "");
}

//D:\Program Files (x86)\TGGame\guser.ini
std::string TGPath::GetConfigFile()
{
    std::string strValue = TGPath::GetInstallPath();
    if (strValue.empty()) return strValue;

    return (strValue + "guser.ini");
}

//C:\Users\Dzlua\AppData\Local\TGGAME
std::string TGPath::GetDataPath()
{
    std::string strValue = TGPath::GetConfigFile();
    if (strValue.empty()) return strValue;

    char data[MAX_PATH];
    ::GetPrivateProfileStringA("USERDIR", "dir", "", data, MAX_PATH, strValue.c_str());

    std::string line = data;
    
    return line.substr(0, line.find("||")) + "\\";
}

//datapath data
//C:\Users\Dzlua\AppData\Local\TGGAME\accouts2.dat
std::string TGPath::GetAccoutsFile()
{
    std::string strValue = TGPath::GetDataPath();
    if (strValue.empty()) return strValue;

    return (strValue + "accouts2.dat");
}